<template>
	<view class="pro-list">
		<view class="pro-list-box">
			<view class="goods-item" v-for="(data,index) in prolist" :key="index" @click="goProInfo(data.goods_id)">
				<view class="goods-item-image">
					<image :src="data.sku.img" mode="aspectFill"></image>
				</view>
				<view class="goods-item-info">
					<view class="goods-item-name">{{data.goods_name}}</view>
					<view class="goods-item-sku">{{data.sku.color}},{{data.sku.lining}},{{data.sku.size}}</view>
					<view class="goods-item-price">
						<text>￥<text>{{data.sku.retail_price}}</text></text>
						<view class="num">x{{data.num}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="pro-list-bot">
			<view class="pro-list-bot-price">
				<view class="pro-list-bot-price-top">共{{prolist.length}}件商品</view>
				<view class="pro-list-bot-price-bottom">合计<text>￥{{totalAmount}}</text></view>
			</view>
			<view class="pro-list-bot-btns">
				<view class="listing" @click="exportInventory">导出</view>
				<view class="add-car" @click="addCar">加入购物车</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				totalAmount: 0,
				prolist: []
			};
		},
		onLoad(options){
			this.prolist = JSON.parse(decodeURIComponent(options.prolist))
			this.prolist.forEach(item => {
				this.totalAmount += item.sku.retail_price * item.num
			})
		},
		methods: {
			exportInventory(){
				let goods = []
				this.prolist.forEach(item => {
					goods.push({
						goods_id: item.goods_id,
						sku_id: item.sku_id,
						goods_number: item.num
					})
				})
				// #ifdef H5
				uni.showLoading({
					title: '正在导出'
				})
				this.$u.api.matchExport({goods_list: JSON.stringify(goods)}).then(res => {
					uni.showToast({
						icon: 'none',
						title: '导出成功'
					})
					window.open(res.datas)
				})
				// #endif
				// #ifdef MP-WEIXIN || APP-PLUS
				uni.showLoading({
					title: '正在导出'
				})
				let name = new Date().getTime()
				this.$u.api.matchExport({goods_list: JSON.stringify(goods)}).then(res => {
					uni.downloadFile({
						url: res.datas,
						// #ifdef MP-WEIXIN
						filePath: wx.env.USER_DATA_PATH + '/' + name + '.xlsx',
						// #endif
						success: (downRes) => {
							// #ifdef MP-WEIXIN
							let filePath = downRes.filePath;
							// #endif
							// #ifdef APP-PLUS
							let filePath = downRes.tempFilePath;
							// #endif
							uni.openDocument({
								filePath: filePath,
								fileType: 'xlsx',
								showMenu: true,
								success: (success) => {
									console.log('打开文档成功', success)
								},
								fail: (fail) => {
									console.log('打开文档失败', fail)
									uni.showToast({
										icon: 'none',
										title: '导出失败，请重试',
									})
								},
								complete: () => {
									uni.hideLoading()
								}
							})
						}
					})
				})
				// #endif
			},
			goProInfo(goods_id){
				getApp().goProInfo(goods_id)
			},
			addCar(){
				if(uni.getStorageSync('userInfo')){
					let cart = []
					this.prolist.forEach(item => {
						cart.push({
							user_id: JSON.parse(uni.getStorageSync('userInfo')).user_id,
							store_id: item.store_id,
							sku_id: item.sku_id,
							num: item.num,
							goods_id: item.goods_id,
							from: 2
						})
					})
					uni.showModal({
						title: '温馨提示',
						content: '是否确认把清单列表的商品加入购物车？',
						success: (confirm) => {
							if(confirm.confirm){
								this.$u.api.AddMatchCart({cart: JSON.stringify(cart)}).then(res => {
									uni.showToast({
										icon: 'none',
										title: '成功加入购物车',
										success: () => {
											setTimeout(() => {
												uni.navigateTo({
													animationDuration: 500,
													url: '/pages/car/car?from=2'
												})
											},1500)
										}
									})
								})
							}
						}
					})
				}else{
					uni.showModal({
						content: '该功能需要登录体验，是否登录？',
						confirmText: '登录',
						cancelText: '取消',
						success: (res) => {
							if(res.confirm){
								let pages = getCurrentPages();
								let curPage = pages[pages.length - 1];
								uni.setStorageSync('newurl',curPage.$page.fullPath)
								// #ifdef APP-PLUS
								uni.navigateTo({
									animationDuration: 500,
									url: '/pages/login/app-login'
								})
								// #endif
								// #ifdef MP-WEIXIN
								uni.navigateTo({
									animationDuration: 500,
									url: '/pages/login/login'
								})
								// #endif
							}
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.pro-list{
		padding-bottom: 120rpx;
		.pro-list-box{
			padding: 30rpx;
		}
		.goods-item{
			display: flex;
			padding: 30rpx 0;
			border-bottom: solid 1px #EEE;
			&:first-child{
				padding-top: 0;
			}
			&:last-child{
				border-bottom: none;
			}
			.goods-item-image{
				width: 240rpx;
				height: 240rpx;
				margin-right: 30rpx;
				image{
					width: 240rpx;
					height: 240rpx;
				}
			}
			.goods-item-info{
				flex: 1;
				min-width: 0;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.goods-item-name{
					font-size: 30rpx;
					color: #141414;
					line-height: 55rpx;
				}
				.goods-item-sku{
					color: #757575;
					font-size: 22rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 3;
					-webkit-box-orient: vertical;
				}
				.goods-item-price{
					display: flex;
					justify-content: space-between;
					align-items: center;
					text{
						color: #141414;
						font-size: 20rpx;
						font-weight: bold;
						text{
							font-size: 28rpx;
						}
					}
					.num{
						color: #757575;
					}
				}
			}
		}
	}
	.pro-list-bot{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		align-items: center;
		height: 120rpx;
		padding: 0 30rpx;
		border-top: solid 1px #EEE;
		background-color: #FFF;
		.pro-list-bot-btns{
			flex: 1;
			min-width: 0;
			text-align: right;
			display: flex;
			justify-content: flex-end;
			font-size: 28rpx;
			.listing{
				height: 80rpx;
				line-height: 80rpx;
				border: solid 1px #000;
				border-radius: 4rpx;
				margin-right: 20rpx;
				padding: 0 15rpx;
			}
			.add-car{
				width: 190rpx;
				height: 80rpx;
				line-height: 80rpx;
				border: solid 1px #000;
				border-radius: 4rpx 0 0 4rpx;
				text-align: center;
				background-color: #141414;
				color: #FFF;
			}
		}
		.pro-list-bot-price{
			display: flex;
			align-items: center;
			.pro-list-bot-price-top{
				color: #5C5C5C;
				font-size: 20rpx;
				color: #5C5C5C;
				border-right: solid 1px #E6E6E6;
				padding-right: 15rpx;
				margin-right: 15rpx;
			}
			.pro-list-bot-price-bottom{
				color: #5C5C5C;
				font-size: 20rpx;
				display: flex;
				align-items: center;
				text{
					font-size: 30rpx;
					font-weight: bold;
					color: #141414;
				}
			}
		}
	}
</style>